package dm.com.dmexamples.image_chooser;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import dm.com.dmexamples.R;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;
import uk.co.senab.photoview.PhotoViewAttacher.OnViewTapListener;

/**
 * @author Damon.Han
 * @Description: 本地图片加载
 * @date 2016年1月7日 下午5:22:46
 */
public class ImageChooserLocalImageDetail extends Fragment {
    public static final String TAG = "ImageChooserLocalImageDetail";
    private Context mContext;
    private PhotoView mImageView;
    private ProgressBar progressBar;
    private PhotoViewAttacher mAttacher;
    private String url;
    private boolean isCancel;

    public static ImageChooserLocalImageDetail newInstance(String url, boolean isCancel) {
        final ImageChooserLocalImageDetail f = new ImageChooserLocalImageDetail();
        final Bundle args = new Bundle();
        args.putString("data", url);
        args.putBoolean("isCancel", isCancel);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            url = bundle.getString("data");
            isCancel = bundle.getBoolean("isCancel", false);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.img_chooser_frg_image_detail, container, false);
        mContext = getContext();
        mImageView = (PhotoView) v.findViewById(R.id.image);
        mAttacher = new PhotoViewAttacher(mImageView);
        mAttacher.setOnViewTapListener(new OnViewTapListener() {

            @Override
            public void onViewTap(View arg0, float arg1, float arg2) {
                if (isCancel) {
                    return;
                }
                getActivity().finish();
            }
        });

        progressBar = (ProgressBar) v.findViewById(R.id.loading);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String urlReal = "";
        if (url.contains("http://") || url.contains("https://")) {
            urlReal = url;
        } else {
            urlReal = "file://" + url;
        }
        progressBar.setVisibility(View.VISIBLE);
        Glide.with(this).load(urlReal).crossFade().listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                Toast.makeText(getContext(), "图片加载失败，请重新尝试..", Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.GONE);
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                mAttacher.update();
                return false;
            }
        }).fitCenter().into(mImageView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
